package com.skeleton.common.base.dto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

/**
 * 结果传输对象
 * 
 * @author Wangyu
 */
public class ResultDto<T extends BaseDto> implements Serializable {

	protected Logger logger = LogManager.getLogger(this.getClass());

	private boolean success = false;
	private String errorMSG;
	private Map<String, List<T>> dtoMap = new HashMap<String, List<T>>();
	private Map<String, Object> otherMap = new HashMap<String, Object>();

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getErrorMSG() {
		return errorMSG;
	}

	public void setErrorMSG(String errorMSG) {
		this.errorMSG = errorMSG;
	}

	public void addToDtoMap(String key, T t) {
		List<T> list = this.dtoMap.get(key);
		if (list == null) {
			list = new ArrayList<T>();
		}
		list.add(t);
		this.dtoMap.put(key, list);
		logger.debug("================================");

		Iterator<String> keys = dtoMap.keySet().iterator();
		while (keys.hasNext()) {
			String k = keys.next();
			List<T> l = dtoMap.get(k);
			logger.debug("  " + k);
			for (T t2 : l) {
				logger.debug("    " + t2);
			}
		}

		logger.debug("================================");
	}

	public List<T> getDtoList(String key) {
		return this.dtoMap.get(key);
	}

	public T getFirstDto(String key) {
		List<T> list = this.dtoMap.get(key);
		return list.get(0);
	}

	public void addToOtherMap(String key, Object o) {
		this.otherMap.put(key, o);
	}

	public Object getOther(String key) {
		return this.otherMap.get(key);
	}

}
